let express = require('express')
let fs = require('fs')

let app = express()

//注册页面
app.get('/',(req,res) =>{
    res.sendfile(__dirname + "/index.html")
})

app.get('/regiser',(req,res) =>{
    let username = req.query.username
    let password = req.query.password
    let user = {
        username:username,
        password:password
    }

    fs.writeFile('./db.json',JSON.stringify(user),() => {


        console.log("存取到db.json文件",user)

        res.redirect('/toLogin')
    })
})

app.get('/toLogin',(req,res)=>{
    res.sendfile(__dirname + "/login.html")
})

app.get('/login',(req,res) =>{
    let username = req.query.username
    let password = req.query.password

    fs.readFile('./db.json',(err,data) =>{
        if(err){
           
            res.sendfile(__dirname + "/error.html")
        }else{
            let str = data.toString()
            let user = JSON.parse(str)

            if(username === user.username && password === user.password){
                res.sendfile(__dirname + "/sucess.html")
            }else{
                res.redirect('/toLogin')
            }
        }
    })
})

app.listen(8081,() =>{
    console.log("statrt sucess")
})